#include "JumperEnemy.h"
#include "../DeadlyShadowsLastHope/Player.h"
#include "../DeadlyShadowsLastHope/DestroyEntityMsg.h"
#include "../GameplayStates/GameplayState.h"


CJumperEnemy::CJumperEnemy(void)
{

}

CJumperEnemy::~CJumperEnemy(void)
{
				CDestroyEntityMsg* pMsg = new CDestroyEntityMsg(this);
		CMessageSystem::GetInstance()->SendMsg(pMsg);
		pMsg = nullptr;
}

//Inherited from CEntity///////////////////////////////
void CJumperEnemy::Update (float fElapsedTime)
{
		if( 0>=GetHealth())
	{
		CDestroyEntityMsg* pMsg = new CDestroyEntityMsg(this);
		CMessageSystem::GetInstance()->SendMsg(pMsg);
		pMsg = nullptr;
	}



	CEntity::Update(fElapsedTime);
	m_pAM->Update(fElapsedTime,this->m_cAniInfo);

	if(this->GetPosY() + this->GetHeight() < CGame::GetInstance()->GetHeight() && this->OnPlatform() == false)
	{
		this->SetVelY(this->GetVelY()+5);
	}
	if(this->GetPosY() + this->GetHeight() > (int)CGame::GetInstance()->GetHeight())
	{
		this->SetPosY((float)CGame::GetInstance()->GetHeight()-this->GetHeight());
	}

		if(this->GetPosX() <= 0)
		if(this->GetVelX() < 0)
		{
			this->SetVelX(0);
			this->SetPosX(0);
		}

		if(this->GetPosX() + this->GetWidth() >= CGame::GetInstance()->GetWidth())
			if(this->GetVelX() > 0)
			{
				this->SetVelX(0);
				this->SetPosX((float)CGame::GetInstance()->GetWidth()-this->GetWidth());
			}
}

void CJumperEnemy::Render (void)
{
		this->m_pD3D->GetSprite()->Flush();
	RECT playerRect = {this->GetRect().left - CGameplayState::GetInstance()->GetCamera().camPosX, this->GetRect().top,this->GetRect().right - CGameplayState::GetInstance()->GetCamera().camPosX, this->GetRect().bottom};
	m_pD3D->DrawRect(playerRect, 0, 255, 0);
m_pAM->Render(m_cAniInfo,playerRect.left-15,playerRect.top+3,1,this->GetFacing());
}

RECT CJumperEnemy::GetRect(void) const
{
	RECT rSelf = {(LONG)GetPosX(),	(LONG)GetPosY(),	(LONG)(GetPosX() + GetWidth()),	(LONG)(GetPosY() + GetHeight())	};
	return rSelf;
}

bool CJumperEnemy::CheckCollision( IEntity* pOther )
{
			if(CEntity::CheckCollision(pOther) == false)
		return false;
	RECT me = this->GetRect();
	RECT other = pOther->GetRect();
	RECT temp;
	IntersectRect(&temp, &me, &other);
	switch(pOther->GetType())
	{
	case OBJ_TILE:
		{
			if(this->GetPosY() <= other.bottom)
				this->SetVelY(this->GetVelY() + 5);
			if(this->GetPosY() >= other.top-3 )
			{
				if (temp.left !=0)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.left-this->GetWidth());
				}

				if(/*temp.right !=0*/ me.left > other.left && me.left < other.right)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.right);
				}
			}
	if(this->GetVelY() > 0)
    {
		if(temp.bottom !=0)
		{
			this->OnPlatform(true);

			if(this->GetVelY() > 0)
				this->SetVelY(0); 
		}
	}
		}
		break;
	case OBJ_PLAYER:
		{
			if(this->GetPosY() <= other.bottom)
				this->SetVelY(this->GetVelY() + 5);
			if(this->GetPosY() >= other.top-3 )
			{
				if (temp.left !=0)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.left-this->GetWidth());
/*
				SOund/damage events
*/ 
				}
				if(/*temp.right !=0*/ me.left > other.left && me.left < other.right)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.right);
					/*
					Send sound and damage events
					*/ 
				}
			}
		}
		break;
	}

	return true;
}
///////////////////////////////////////////////////////
bool  CJumperEnemy::Init(CEntity* pTarget,int x,int y,int speedset)
{
			if(pTarget->GetType() != OBJ_PLAYER)
		return false;
		else
		{
	m_pES = CEventSystem::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();
	//m_pXA	= CSGD_XAudio2::GetInstance();
	m_pD3D	= CSGD_Direct3D::GetInstance();

	m_pMS	= CMessageSystem::GetInstance();
	m_pAM	= CAnimationManager::GetInstance();

	CEventSystem::GetInstance()->RegisterClient("take damage", this);
	CEventSystem::GetInstance()->RegisterClient("Return", this);


	//Set planned animations
	this->m_cAniInfo.SetName("EJumpWalk");		 //Specific to class of enemy
	this->m_cAniInfo.SetNext("EJumpAttack");	 //Specific to class of enemy
	this->m_cAniInfo.SetTime(0);			 //Specific to class of enemy
	this->m_cAniInfo.SetFrame(0);			 //Specific to class of enemy

	this->SetHeight(32);
	this->SetWidth(32);

	this->SetFacing(true);
	this->SetDensity(1);

	this->SetPosX(float(x));
	this->SetPosY(float(y));
	this->m_eType = OBJ_JUMPER_ENEMY;
	this->SetVelX(0);
	this->SetVelY(0);
	this->m_pTarget = pTarget;
	this->m_nHealth = 100;
	this->m_nSpeedoffset =speedset;
	this->OnPlatform(false);

	this->m_fTimer =0;
	this->m_fAttackTimer =0.75f;
	this->m_battack=false;
	}
	return true;
}

void  CJumperEnemy::HandleEvent(CNEvent* pEvent)
{
		if(pEvent->GetEventID() == "take damage")
	{
		this->m_nHealth -= 35;
		CPlayer* pPlayer = dynamic_cast<CPlayer*>(this->m_pTarget);
		pPlayer->SetCurrentHits(pPlayer->GetCurrentHits()+1);
		this->SetVelX(-(this->GetVelX()+10));
	}
	if(pEvent->GetEventID() == "Return")
	{
		CEventSystem* self = CEventSystem::GetInstance();
		self->SendEvent("TakePlayerDamage");
		m_cAniInfo.SetName("EJumpWalk");
		m_cAniInfo.SetNext("EJumpAttack");
		m_cAniInfo.SetTime(0);
		m_cAniInfo.SetFrame(0);
		m_battack=false;


	}
}
